<template>
	<view>
		<u-row customStyle="margin-bottom: 10px">
			<u-col span="12">
				<u-alert
					title="寝室号格式为:\neg:8公寓C区999 则填写 8C999 \neg:5公寓999(无区域) 则填写 5-999"
					type = "info"
					description=">填写完成后,需要点击<查询>按钮;\n>若寝室号填写并无问题,则显示寝室电量,\n此时你可以点击<确定>按钮保存,\n>否则寝室号错误,需要您重新填写"
					></u-alert>
			</u-col>
		</u-row>
		<u-row customStyle="margin-bottom: 10px">
			<u-col span="1"></u-col>
			<u-col span="10">
				<u--input
				    placeholder="请输入寝室号"
				    border="surround"
				    v-model="room"
				  ></u--input>
			</u-col>
			<u-col span="1"></u-col>
		</u-row>
		<u-row customStyle="margin-bottom: 10px">
			<u-col span="1"></u-col>
			<u-col span="10">
				<u-button type="primary" text="查询" @click="csSubmit()"></u-button>
			</u-col>
			<u-col span="1"></u-col>
		</u-row>
		<u-row customStyle="margin-bottom: 10px">
			<u-col span="1"></u-col>
			<u-col span="10">
				<u-button type="error" :disabled="isSubmitButton" @click="bcSubmit()" text="确定"></u-button>
			</u-col>
			<u-col span="1"></u-col>
		</u-row>
		<u-row customStyle="margin-bottom: 10px">
			<u-col span="1"></u-col>
			<u-col span="10">
				<u--text align="center" :text="textState"></u--text>
			</u-col>
			<u-col span="1"></u-col>
		</u-row>
	</view>
</template>

<script>
	import { data } from '../../../uni_modules/uview-ui/libs/mixin/mixin.js';
	import {AES_Decryptecb, AES_Encryptecb} from '../../index/ECB.js'
	import {mixin} from '../../../mixin.js'
	export default {
		data() {
			return {
				room:'',
				roomNum:'',
				isSubmitButton:true,
				isErr:0,
				roomId:'',
				xiaoquId:''
			};
		},
		mixins:[mixin],
		onLoad() {

		},
		computed:{
			textState(){
				if(this.isErr==1){
					return '当前状态：'+'输入的寝室号格式不对或错误'
				}else if(this.isErr==2){
					return '获取成功：寝室电量为'+this.roomNum+'\n可点击确定按钮保存'
				}else if(this.isErr==3){
					return '保存成功'
				}
				if(this.room == ''){
					return '当前状态：'+'寝室号为空'
				}else if(this.room!=''){
					return '当前状态：'+'正在输入'
				}
				return ''
			}
		},
		methods:{
			csSubmit(){
				if(this.room==''){return}
				this.getRoomNum()
			},
			bcSubmit(){
				uni.setStorageSync("room",this.room)
				uni.setStorageSync("xiaoquId",this.xiaoquId)
				uni.setStorageSync("roomId",this.roomId)
				this.isErr = 3
				uni.showToast({
					title:"保存成功"
				})
				uni.reLaunch({
					url:'../../index/index'
				})
			},
			//获取寝室电量
			getRoomNum(){
				let roomO = {
					sno: '4122013603',
					xiaoquId: '',
					loudongId: '',
					roomId: 14366,
					fullroomname: this.room
				}
				uni.request({
					url:'http://vpn.tdhzyun.com/tdwx-fr/ywcl/getRoomList',
					method:"POST",
					headers: {
					    //cookie: 'JSESSIONID=1D5A641948086EC5570030E02847FDCC',
					    Host: 'vpn.tdhzyun.com',
					    Connection: 'keep-alive',
					    Accept: 'application/json, text/plain, */*',
					    accessToken: '',
					    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
					    'Content-Type': 'application/json',
					    Origin: 'http://vpn.tdhzyun.com',
					    Referer: 'http://vpn.tdhzyun.com/person',
					    'Accept-Language': 'zh-CN,zh;q=0.9',
					    'Accept-Encoding': 'gzip, deflate',
					    'Content-Length': '87'
					  },
					  data: {
					    sno: '4122013603',
					    xiaoquId: '',
					    loudongId: '',
					    roomId: '',
					    fullroomname: this.room,
						sign: AES_Encryptecb(JSON.stringify(roomO)+"1234567890ABCDEF"+this.getMd5(JSON.stringify(roomO)))
					  },
					  success:res=>{
						  console.log(res)
						  if(res.statusCode==200){
							  let obj = JSON.parse(AES_Decryptecb(res.data.returnObject)).list[0]
							  if(obj==undefined){
								  this.isErr = true
								  return
							  }
							  if(JSON.parse(AES_Decryptecb(res.data.returnObject)).list.length > 1){
								  this.isErr = true
								  return
							  }
							  let num =(obj.allamp - obj.usedamp).toFixed(2)
							  //console.log(num)
							  this.roomNum = num
							  this.isErr = 2
							  this.isSubmitButton = false
							  this.xiaoquId = obj.xiaoquId
							  this.roomId = obj.roomId
						  }
					  },
					err:e=>{
						console.log(e)
					}
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
